﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Validation;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace KPIS.GERP.WCF.ePayment.Models
{
    [DataContract, Table("InvoiceItem")]
    public class InvoiceItem
    {
        public InvoiceItem()
        {
            deleted = false;
        }

        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [DataMember, Display(Name = "invoice_item_id")]
        public int invoice_item_id { get; set; }

        [ForeignKey("Invoice")]
        [DataMember, Display(Name = "invoice_id")]
        public int invoice_id { get; set; }


        [DataMember, Display(Name = "product_id")]
        public int? product_id { get; set; }

        [DataMember, Display(Name = "รหัสสินค้า/บริการ")]
        [MaxLength(20)]
        public string product_code { get; set; }

        [DataMember, Display(Name = "ชื่อสินค้า/บริการ")]
        [MaxLength(70)]
        public string product_name { get; set; }

        [DataMember, Display(Name = "จำนวน")]
        [DecimalPrecision(15,2)]
        public double qty { get; set; }

        [DataMember, Display(Name = "ราคาไม่รวม vat (บาท)")]
        [DecimalPrecision(15, 2)]
        public double exclude_vat_price { get; set; }

        [DataMember, Display(Name = "ราคารวม vat (บาท)")]
        [DecimalPrecision(15, 2)]
        public double include_vat_price { get; set; }

        [DataMember, Display(Name = "จำนวน vat ในรายการนี้ (บาท)")]
        [DecimalPrecision(15, 2)]
        public double vat_amount { get; set; }

        [DataMember, Display(Name = "ส่วนลด (บาท)")]
        [DecimalPrecision(15, 2)]
        public double discount_amount { get; set; }

        [DataMember, Display(Name = "จำนวนเงินไม่รวม vat (บาท)")]
        [DecimalPrecision(15, 2)]
        public double exclude_vat_amount { get; set; }

        [DataMember, Display(Name = "จำนวนเงินรวม vat (บาท)")]
        [DecimalPrecision(15, 2)]
        public double include_vat_amount { get; set; }

        [DataMember, Display(Name = "หน่วยนับ")]
        [Required]
        public string unit { get; set; }

        [DataMember, Display(Name = "หมายเหตุ")]
        public string remark_1 { get; set; }

        [DataMember, Display(Name = "doc_id")]
        public string doc_id { get; set; }

        [DataMember, Display(Name = "วันที่สร้างรายการ")]
        [Required]
        public DateTime created_when { get; set; }

        [DataMember, Display(Name = "วันที่แก้ไขรายการ")]
        public DateTime? updated_when { get; set; }

        [DataMember, Display(Name = "ลบ")]
        public bool deleted { get; set; }

        [DataMember, Display(Name = "วันที่ลบรายการ")]
        public DateTime? deleted_when { get; set; }

        public virtual Invoice Invoice { get; set; }




        public int Create(InvoiceItem model)
        {
            using(ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted = false;
                    model.created_when = DateTime.Now;

                    context.InvoiceItems.Add(model);
                    context.SaveChanges();

                    return model.invoice_id;
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Edit(InvoiceItem model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.updated_when = DateTime.Now;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }

        public void Delete(InvoiceItem model)
        {
            using (ePaymentContext context = new ePaymentContext())
            {
                try
                {
                    model.deleted_when = DateTime.Now;
                    model.deleted = true;

                    context.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }
    }
}